package handler

import (
	"github.com/name5566/leaf/gate"
	"server/game/bag"
	"server/msg/jsonData"
)

func AddItem(args []interface{}) {
	m := args[0].(*jsonData.MsgAddItemReq)
	a := args[1].(gate.Agent)
	backLogic := bag.NewBackpack(m.UserId, bag.Bagsiz)
	if len(backLogic.Items) >= backLogic.Size {
		a.WriteMsg(&jsonData.MsgRes{Ret: jsonData.ErrCode(10), RequestId: m.RequestId})
		return
	}
	item := backLogic.AddItem(m.UserId, m.ItemTmpId, m.Quantity)
	a.WriteMsg(&jsonData.MsgRes{Ret: jsonData.ErrCode(0),
		Result: item, RequestId: m.RequestId})
}
func AddItemsByXiaoYou(args []interface{}) {
	m := args[0].(*jsonData.MsgAddItemsByXiaoYouReq)
	a := args[1].(gate.Agent)
	backLogic := bag.NewBackpack(m.UserId, bag.Bagsiz)
	for _, itemInfo := range m.ItemIds {
		backLogic.AddItem(m.UserId, itemInfo.ItemId, itemInfo.Num)
	}
	a.WriteMsg(&jsonData.MsgRes{
		Ret:       0,
		Result:    backLogic.ListItems(),
		RequestId: m.RequestId,
	})
}
func UserItem(args []interface{}) {
	m := args[0].(*jsonData.MsgUseItemReq)
	bagLogic := bag.NewBackpack(m.UserId, bag.Bagsiz)
	item, check := bagLogic.GetItem(m.ItemId)
	a := args[1].(gate.Agent)
	if !check || item.Quantity <= 0 {
		a.WriteMsg(&jsonData.MsgRes{Ret: jsonData.ErrCode(10), RequestId: m.RequestId})
		return
	}
	err := bagLogic.UseItem(item.ItemTmpID, m.Quantity)
	if err != nil {
		a.WriteMsg(&jsonData.MsgRes{Ret: 1, Message: err.Error(), RequestId: m.RequestId})
	}
	itemList := bagLogic.ListItems()
	a.WriteMsg(&jsonData.MsgRes{Ret: jsonData.ErrCode(0), Result: itemList, RequestId: m.RequestId})
}
func BagList(args []interface{}) {
	m := args[0].(*jsonData.MsgBagListReq)
	backLogic := bag.NewBackpack(m.UserId, bag.Bagsiz)
	itemList := backLogic.ListItems()
	a := args[1].(gate.Agent)
	a.WriteMsg(&jsonData.MsgRes{Ret: jsonData.ErrCode(0), Result: itemList, RequestId: m.RequestId})
}
